<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/6/26
 * Time: 14:02
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\User;
use common\models\DAO\UserInvest;

class Homeservice {

    public static function init(){
        $new = FundService::getInfo();
        $new['name'] = '新手标';
        $new['url'] = '/sprog.html';
        $new['info'] = '1分体验，狂息返现';
        $new['rate'] = CommonFunction::formatMoney($new['rate']);
        $list = DayService::getTotal();
        $result['list'] = array_merge([0=>$new], $list);
        $result['banner'] = AdService::getBanner(2);
        return $result;
    }
    public static function app_init($appKey = []){
        $least_invest = \Yii::$app->params['least_invest'];
        $result['least_invest'] =  $least_invest/ 100;
        $result['init_money'] = $least_invest/100;
        $remainder = 0;
        $hongbao_remainder = 0;
        $userInfo = [];
        $userModel = [];
        $appKey && $userModel = User::findOne(['appKey'=>$appKey]);
        $userModel && $userInfo = $userModel->attributes;
        if($userInfo){
            $isNew = $userInfo['isNew'];
            $userID = $userInfo['id'];
            if($isNew == 1){
                $invest = UserInvest::findOne(['userID'=>$userID]);
                if($invest){
                    $userModel->isNew = 0;
                    $userModel->save();
                    $isNew = 0;
                }
            }
            if($isNew == 1){
                $fund = FundService::get();
                $fund['rate'] = CommonFunction::formatMoney($fund['rate']);
                $result['fund'] = $fund;
            }
            $hongbao = HongBaoService::get($userID);
            $hongbao > 0 && $hongbao_remainder = $hongbao;
            $user['hongbao'] = $hongbao;
            $remainder = Wallet::remainder($userID);
            if($remainder > 0 && $remainder > $least_invest)
                $result['init_money'] = $remainder/100;
            $user['remainder'] = $remainder;
            $result['user'] = $user;
            $result['coupon'] = CouponService::get($userID);
            if($isNew == 1){
                $fund_init = $result['init_money'];
                $result['fund']['init_money'] = $fund_init;
            }
        }else{
            $fund = FundService::get();
            $fund['rate'] = CommonFunction::formatMoney($fund['rate']);
            $result['fund'] = $fund;
        }
        $index = 1;
        $_result = [];
        $flag = 6;
        $date = intval(date('Ymd'));
        $end_date = 20160419;
        if($date > $end_date)
            $flag = 5;
        while($index <= $flag){
            $cache = DayService::get($index);
            $cache['rate'] = CommonFunction::formatMoney($cache['rate']);
            $cache_day = $cache['day'];
            $cache_day < 30 && $cache_day = 30;
            $cache['hb_scale'] = HongBaoService::setting($cache_day);
            $cache['init_hongbao'] = intval($least_invest/$cache['hb_scale']);
            if($remainder > 0){
                $cache['init_hongbao'] = intval($remainder/$cache['hb_scale']);
            }
            if($cache['init_hongbao'] > $hongbao_remainder)
                $cache['init_hongbao'] = $hongbao_remainder;
            $_result[] = $cache;
            $index ++ ;
        }
        if($date <= $end_date){
            array_unshift($_result, $_result[count($_result) - 1]);
            unset($_result[count($_result) - 1]);
        }
        $result['list'] = $_result;
        return $result;
    }

    public static function simple($userID){
        $index = 1;
        $_result = [];
        $least_invest = \Yii::$app->params['least_invest'];
        $result['least_invest'] = $least_invest / 100;
        $remainder = Wallet::remainder($userID);
        $user['remainder'] = $remainder;
        $hongbao = HongBaoService::get($userID);
        $user['hongbao'] = $hongbao;
        $result['user'] = $user;
        $result['coupon'] = CouponService::get($userID);
        /* $day = DayService::get(1);
         $day['rate'] = CommonFunction::formatMoney($day['rate']);
         $result['day'] = $day;*/
        $result['least_invest'] =  $least_invest/ 100;
        $result['init_money'] = $least_invest;
        if($remainder > 0)
            $result['init_money'] = $remainder;
        $flag = 6;
        $date = intval(date('Ymd'));
        $end_date = 20160419;
        if($date > $end_date)
            $flag = 5;
        while($index <= $flag){
            $cache = DayService::get($index);
            $cache['rate'] = CommonFunction::formatMoney($cache['rate']);
            $cache_day = $cache['day'];
            $cache_day < 30 && $cache_day = 30;
            $cache['hb_scale'] = HongBaoService::setting($cache_day);
            $cache['init_hongbao'] = intval($least_invest/$cache['hb_scale']);
            if($remainder > 0){
                $cache['init_hongbao'] = intval($remainder/$cache['hb_scale']);
            }
            if($cache['init_hongbao'] > $hongbao)
                $cache['init_hongbao'] = $hongbao;
            $_result[] = $cache;
            $index ++ ;
        }
        if($date <= $end_date){
            array_unshift($_result, $_result[count($_result) - 1]);
            unset($_result[count($_result) - 1]);
        }
        $result['list'] = $_result;
        return $result;
    }

    public static function getFunk(){

    }

}